<template>
  <div>
    <el-tree
      default-expand-all
      ref="tree"
      node-key="id"
      :show-checkbox="true"
      :check-strictly="true"
      :data="list"
      :props="{ label: 'name', children: 'children' }"
    />
    <el-row type="flex" height="60" justify="end">
      <el-button type="primary" @click="hSubmit()">确定</el-button>
      <el-button type="info" @click="$emit('close')">取消</el-button>
    </el-row>
  </div>
</template>
<script>
// loadPermissionList
import { getPermissionList } from '@/api/permissions'
import { transDataToTreeData } from '@/utils'
import {getRolesDetail,assignPrem} from '@/api/settings'
export default {
  props:{
    roleId:{
      type:String,
      required:true
    }
  },
  data() {
    return {
      list: [] // 权限站点列表
    }
  },
  created() {
    this.loadPermissionList()
  },
  
  methods: {
    //分配权限
    async hSubmit(){
      try {
        //获取节点中的id
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPrem({id:this.roleId,permIds:permIds})
        console.log('assignPrem', res)
        //关闭
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log('assignPrem', err)
      }
    },
    // 获取数据
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        // 将数据转换为树形结构
        this.list = transDataToTreeData(res.data)
        const info = await getRolesDetail(this.roleId)
        console.log('info',info);
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>

<style scoped></style>
